package liziy.spring.context.support;

import liziy.spring.context.ApplicationContext;
import liziy.spring.beans.factory.config.BeanDefinition;
import liziy.spring.beans.BeanDefinitionRegistry;
import liziy.spring.beans.support.DefaultListableBeanFactory;

/**
 * 基于注解的启动器
 * @author Liziy
 * @date 2020/12/15 23:04
 **/
public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry  {
    //    甩钩模式
    /**
     * 定义默认容器属性
     */
    private DefaultListableBeanFactory beanFactory;
    private ApplicationContext parent;

    public GenericApplicationContext(){
        //初始化spring默认容器
        this.beanFactory = new DefaultListableBeanFactory();
    }

    public GenericApplicationContext(ApplicationContext parent){
        this.parent = parent;
    }

    @Override
    public int registryBeanDefinition(String beanName, BeanDefinition definition) {
        return this.beanFactory.registryBeanDefinition(beanName, definition);
    }

    public DefaultListableBeanFactory getBeanFactory(){
        return beanFactory;
    }

    @Override
    public ApplicationContext getParent() {
        return this.parent;
    }

    @Override
    public Object getBean(String beanName) {
        return this.beanFactory.getBean(beanName);
    }
}
